﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Processes.Orden
{
    public class Leaf
    {
        public string text;
        public int number;
        public Leaf[] children;
        private int limit;
        int quantity;
        public List<string> allABB;

        public Leaf last;
        public Leaf()
        {
            limit= 100;
            quantity = 0;
            children = new Leaf[limit];
            number = 1;
            for (int i = 0; i < limit; i++)
            {
                children[i] = null;
            }
            allABB = new List<string>();
        }
        public void printAbb(Leaf l,string lastNum)
        {
            if (l != null)
            {
                string numberPass = lastNum;
                string s = "";
                if (lastNum != "" && lastNum != " ")
                    numberPass = " "+lastNum + l.number + ".";
                else
                {
                    if (lastNum == "")
                    {
                        numberPass = " ";
                    }
                    else
                    {
                        numberPass = " "+l.number.ToString()+".";
                    }
                }
                    
                s += numberPass + " " + l.text;
                allABB.Add(s);
                for (int i = 1; i <= l.quantity; i++)
                {
                    printAbb(l.children[i],numberPass);
                }
            }
        }
        public void modificar(List<int> number, Leaf actual, string text)
        {

            if (actual != null)
            {
                int pos;
                number.RemoveAt(0);
                if (number.Count >= 1)
                {

                    pos = number[0];
                    modificar(number, actual.children[pos], text);
                }
                else
                {

                    actual.text = text;
                    
                }
            }

        }
        public void eliminar(List<int> number, Leaf actual)
        {
            if (actual != null)
            {
                int pos;
                number.RemoveAt(0);
                if (number.Count > 1)
                {
                    pos = number[0];
                    last = actual.children[pos];
                    eliminar(number, actual.children[pos]);
                }
                else
                {
                    desplazarYEliminar(number[0], last);
                }
            }
        }
        public void insert(List<int> number, Leaf actual,string text)
        {
            if (actual != null)
            {
                int pos;
                number.RemoveAt(0);
                if (number.Count > 1)
                {
                    
                    pos = number[0];
                    last = actual.children[pos];
                    insert(number, actual.children[pos], text);
                }
                else
                {
                    if (actual.children[number[0]] == null)
                    {
                        actual.children[number[0]] = new Leaf();
                        actual.children[number[0]].number = number[0];
                        actual.children[number[0]].text = text;
                        actual.quantity++;
                    }
                    else
                    {
                        Leaf myLeaf = new Leaf();
                        myLeaf.text = text;
                        myLeaf.number = number[0];
                        displaceAndInsert(number[0], myLeaf,last);
                    }
                }
            }
            
        }
        
        public void desplazarYEliminar(int index,Leaf last)
        {
            for (int i = index; i < last.quantity; i++)
            {
                last.children[i] = last.children[i + 1];
                last.children[i].number--;
            }
            last.children[last.quantity] = null;
            last.quantity--;
        }
        public void displaceAndInsert(int index,Leaf leaf,Leaf last)
        {
            last.children[last.quantity+1] = new Leaf();
            for (int i = last.quantity; i >= index; i--)
            {
                last.children[i + 1] = last.children[i];
                last.children[i + 1].number++;
            }
            last.children[index] = leaf;
            last.quantity++;
        }
    }
}
